package com.WebProject.web;

import com.WebProject.control.AdminDao.AdminControlDao;
import com.WebProject.control.AdminDao.Impl.AdminControlDaoImpl;
import com.WebProject.control.UserDao.Impl.UserControlDaoImpl;
import com.WebProject.control.UserDao.UserControlDao;
import com.WebProject.pojo.*;
import com.WebProject.service.AdminDao.AadminServiceDao;
import com.WebProject.service.AdminDao.impl.AdminServiceDaoImpl;
import com.WebProject.service.UserDao.UserServiceDao;
import com.WebProject.service.UserDao.impl.UserSerbiceDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;

public class UserCarServlet extends BaseServlet{

    private UserControlDao mapper = new UserControlDaoImpl();
    private AdminControlDao adminMapper = new AdminControlDaoImpl();

    protected void userlist(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ArrayList<t_car> cars = new ArrayList<>();
        ArrayList<t_brand> brands = adminMapper.QueryAllBrandInfo();
        ArrayList<t_category> categories = adminMapper.QueryAllCategoryInfo();
        req.setAttribute("brands",brands);
        req.setAttribute("categories",categories);

        String brand_Name = req.getParameter("brand_Name");
        String category_Name = req.getParameter("category_Name");
        String order = req.getParameter("order");
        if (order != null){
            cars = mapper.QueryCar(order);
        } else if (brand_Name != null && category_Name != null && !brand_Name.equals("无") && !category_Name.equals("无")){
            cars = mapper.QueryAllBrandAndCategory(brand_Name,category_Name);
        } else if (brand_Name != null && !brand_Name.equals("无")){
            cars = mapper.QueryAllBrandCar(brand_Name);
        } else if(category_Name!=null && !category_Name.equals("无")){
            cars = mapper.QueryAllTypeCar(category_Name);
        } else {
            cars = mapper.QueryAllCarInfo();
        }
        if (cars == null || cars.size() == 0){
            req.setAttribute("msg","没有数据！");
        }
        req.setAttribute("cars", cars);

        req.getRequestDispatcher("/pages/UserPage/carList.jsp").forward(req,resp);
    }

    protected void userqueryZu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = (String) req.getSession().getAttribute("username");

        ArrayList<t_car_zu> t_car_zus = mapper.QuerUserZuInfo(username);

        req.setAttribute("zus",t_car_zus);
        if (t_car_zus == null||t_car_zus.isEmpty()){
            req.setAttribute("msg","没有该数据！");
        }
        req.getRequestDispatcher("/pages/UserPage/queryZu.jsp").forward(req,resp);
    }
    protected void Zu(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = (String) req.getSession().getAttribute("username");

        String car_Number = req.getParameter("car_Number");

        if (!mapper.updateZuCarStatus(username,car_Number)){
            req.setAttribute("msg","租车失败");
        }
        resp.sendRedirect(req.getContextPath() + "/User/UserCarServlet?action=userlist");
    }

    protected void ZuOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = (String) req.getSession().getAttribute("username");

        ArrayList<t_car_zu> zus = mapper.QueryCarOrder(username);

        req.setAttribute("zus",zus);
        if (zus == null || zus.isEmpty()){
            req.setAttribute("msg","租车失败");
        }
        req.getRequestDispatcher("/pages/UserPage/ZuOrder.jsp").forward(req,resp);
    }

    protected void Huan(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String car_Number = req.getParameter("car_Number");

        if (!mapper.updateHuanCarStatus(car_Number)){
            req.setAttribute("msg","还车失败请重新尝试");
        }

        userqueryZu(req,resp);
    }
    protected void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String forget = req.getParameter("aa");
        String username = req.getParameter("username");
        req.getSession().setAttribute("username",username);
        String password = req.getParameter("password");
        String confirmPassword = req.getParameter("ConfirmPassword");
        String sex = req.getParameter("sex");
        String id_number = req.getParameter("id");
        String tel = req.getParameter("tel");
        String addr = req.getParameter("address");
        Integer sexid;

        if (forget != null && forget.equals("forget")){
            t_user user = mapper.QueryUserInfo(username);
            if (user!=null){
                String tel1 = user.getTel();
                String id_number1 = user.getId_number();
                if (!tel.equals(tel1) && id_number.equals(id_number1)){
                    req.setAttribute("msg","信息不正确");
                    req.setAttribute("username",username);
                    req.setAttribute("id",id_number);
                    req.getRequestDispatcher("/pages/UserPage/forgetUser.jsp").forward(req,resp);
                }
                req.getRequestDispatcher("/pages/UserPage/changePassword.jsp").forward(req,resp);
            }
            req.setAttribute("msg","没有该用户");
            req.getRequestDispatcher("/pages/UserPage/forgetUser.jsp").forward(req,resp);

        }

        else {
            if(!password.equals(confirmPassword)){
                req.setAttribute("msg","两次密码不一致");
            }
            else {
                if (sex.equals("男")){
                    sexid = 0;
                }else {
                    sexid = 1;
                }
                //封装
                t_user user = new t_user(null,username,password,sexid,id_number,tel,addr,null);
                //该用户存在
                if (!mapper.registerUser(user)){
                    req.setAttribute("msg","该用户已存在");
                }
            }
            //注册成功&不成功
            req.getRequestDispatcher("/index.jsp").forward(req,resp);
        }

    }
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = (String) req.getSession().getAttribute("username");

        if (!mapper.deleteUser(username)){
            req.setAttribute("msg","注销失败请再尝试");
        }
        req.getRequestDispatcher("/index.jsp").forward(req,resp);
    }
    protected void changepassword(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String username = (String)req.getSession().getAttribute("username");
        String password = req.getParameter("password");

        t_user user = mapper.QueryUserInfo(username);


        if (user !=null && user.getPassword().equals(password)){
            req.setAttribute("msg","新密码不能与旧密码相同");
            req.getRequestDispatcher("/pages/UserPage/changePassword.jsp").forward(req,resp);
        }
        if (!mapper.changePassword(username,password)){
            req.setAttribute("msg","修改失败,请再次尝试");
        }
        req.getRequestDispatcher("/index.jsp").forward(req,resp);

    }

}
